package controller

import (
	"ce-cloud-service/internal/proto/base/user"
	"ce-cloud-service/internal/services/gate/hall/msg"
	"ce-cloud-service/internal/services/gate/hall/service"
	"ce-cloud/pkg/log"
	"errors"
	"github.com/gin-gonic/gin"
)

// UserRegister
// @tags User
// @summary 注册
// @param data body msg.UserRegisterRequest true "请求参数"
// @success 200 {object} sendMsgToHttpRspData "响应参数"
// @router /user/register [post]
func UserRegister(gtx *gin.Context) {
	req := new(msg.UserRegisterRequest)
	if err := gtx.Bind(&req); err != nil {
		log.Error("error")
	}
	if req.Password != req.ConfirmPwd {
		log.Errorf("密码不一致 req.Pwd:%s, req.ConfirmPwd:%s", req.Password, req.ConfirmPwd)
		sendMsgToHttp(gtx, nil, errors.New("密码不一致"))
		return
	}
	rsp, err := service.Only().UserService.Register(gtx, &user.UserRegisterRequest{
		Account:  req.Account,
		Password: req.Password,
	})
	if err != nil {
		log.Error("err：", err)
		sendMsgToHttp(gtx, nil, err)
		return
	}
	sendMsgToHttp(gtx, rsp, nil)
}

// UserLogin
// @tags User
// @summary 登录
// @param data body msg.UserLoginRequest true "请求参数"
// @success 200 {object} msg.UserLoginResponse "响应参数"
// @router /user/login [post]
func UserLogin(gtx *gin.Context) {
	req := new(msg.UserLoginRequest)
	if err := gtx.Bind(req); err != nil {
		log.Error("error")
	}
	log.Infof("login request: %+v", req)
	if req.Password == "" {
		log.Error("password is empty")
		sendMsgToHttp(gtx, "", errors.New("password is empty"))
		return
	}
	_, sign, err := service.Only().UserService.Login(gtx, &user.UserLoginRequest{
		Account:  req.Account,
		Password: req.Password,
	})
	if err != nil {
		log.Error("err：", err)
		sendMsgToHttp(gtx, nil, err)
		return
	}
	sendMsgToHttp(gtx, msg.UserLoginResponse{Sign: sign}, nil)
}
